package cn.yunlang.zhangZhongFei.controller;

import cn.yunlang.zhangZhongFei.entity.SysUser;
import cn.yunlang.zhangZhongFei.entity.User;
import cn.yunlang.zhangZhongFei.entity.req.WxCodeReq;
import cn.yunlang.zhangZhongFei.service.LoginService;
import cn.yunlang.zhangZhongFei.vo.response.LoginResponseVo;
import cn.yunlang.zhangZhongFei.vo.response.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "登录模块")
@Controller
@RestController
@RequestMapping("/login")
public class LoginController {
    @Resource
    private LoginService loginService;

    @ApiOperation("用户通过code登录")
    @PostMapping("/login")
    public R<LoginResponseVo> login(@RequestBody WxCodeReq code) {
        String openId = loginService.loginWeixin(code);
        LoginResponseVo loginResponseVo = loginService.userLogin(openId);
        return R.success(loginResponseVo);
    }

    @ApiOperation("根据code获取Phone")
    @PostMapping("/getPhone")
    public R<String> getPhone(@RequestBody WxCodeReq code) {
        return R.success(loginService.getPhone(code));
    }


    @ApiOperation("后端测试用 前台用户 登录接口(用户手机号唯一)")
    @PostMapping("/backLogin")
    public R<LoginResponseVo> backLogin(@RequestBody User user) {
        return R.success(loginService.backLogin(user));
    }


    @ApiOperation(value = "后台登录")
    @PostMapping("/sys")
    public R<LoginResponseVo> sys(@RequestBody SysUser user) {
        return R.success(loginService.sysLogin(user));
    }

    @ApiOperation(value = "后台退出")
    @PostMapping("/logout")
    public R<?> logout() {
        loginService.logout();
        return R.success();
    }
}
